function uvvisslicenew(datasetname,nx,ny,wl,ascii)
% 
% function uvvisslice(datasetname,nx,ny,wl)
%   datasetname : Filename string, extension '.mat' is assumed
%                 Need to have run uvvisload first
%   nx : Number of CCD pixels to bin
%   ny : Number of time slices to bin
%   wl : Low wavelength of bin slice [nm]
%   ascii : if ==1 data is saved as ASCII, TAB delimited
% 
% Bin kinetic slice of ANDOR ASCII kinetic dataset, then plot
% 
% Example:
%   >> uvvisslice('p450_20100707_run1', 10, 50, 413)

% V2.0, F. Dworkowski & M. Fuchs 2011

%% PLEASE MODIFIY plotxx TO CONTAIN axis tight FOR BOTH PLOTS TO ENSURE 
%% CORRECT OVERLAY OF BOTH AXIS
% Help if called without argument
if nargin<4,
    help uvvisslicenew
    return
end

try load(datasetname)
catch error,
  disp(char('Ooops! :',' ',error,' ','Need to have run uvvisload first'))
  return
end

%% -- Pre-Calculations ----------------------------------------------------
datay=datay';

ix=max(find(datax<=wl));  % Find pixel belonging to slice wavelength
timef=char(footer.KineticCycleTime);

datay=datay(1:(min(find(datay(:,end)<1e-6))-1),:);  % Cut empty lines

%% -- bin in slices -------------------------------------------------------
maxnx = length(datax)/2;

if nx > 1 && (ix+nx) < maxnx 
    slicex=mean(datay(:,ix:ix+nx-1),2); % Bin kinetic slice

elseif (ix+nx) >= maxnx
     he=errordlg({'Can not bin that much in x' 'Using unbinned data instead'});
%     uiwait(he)
%     return
    slicex=mean(datay(:,ix),2);         % Bin kinetic slice
elseif nx == 1
    slicex=mean(datay(:,ix),2);         % Bin kinetic slice
else
    he=errordlg('Something wrong with nx');
    uiwait(he)
    return
end

%% -- bin in time axis ----------------------------------------------------
maxny = length(datay(:,1))/2;
if ny > 1 && ny <= maxny
    slicexyb=rowbin(slicex,ny,1);           % Bin time points
elseif ny > maxny
    he=errordlg('Something wrong with ny');
    uiwait(he)
    return
else 
    slicexyb = slicex;
end

x1=ny*(1:length(slicexyb));
x2=ny*str2num(timef)*(1:length(slicexyb));

figure %(1)
 xlabels{1} = 'Time Slice';
 xlabels{2} = 'time (s)';
 ylabels{1} = 'AU';
 ylabels{2} = 'AU';
plotxx(x1, slicexyb, x2 , slicexyb, xlabels, ylabels)
title(['Kinetic slice wl = ',num2str(wl),' nm, xbin ',num2str(nx),', ybin ',num2str(ny)])


%% -- ASCII Output if desired ---------------------------------------------
if ascii==1
    newdataname=([datasetname,'_sliced_at_',num2str(wl),'nm.asc']);

    newData(:,1)=x1;
    newData(:,2)=x2;
    newData(:,3)=slicexyb;
    dlmwrite(newdataname, newData, 'delimiter', ',');
end


function AvgF = rowbin(a,nbin,ncol)
% From
% http://stackoverflow.com/questions/1787057/how-should-i-perform-this-binning-and-averaging-in-matlab

nRemove = rem(size(a,1),nbin); % Find the number of points to remove
a = a(1:end-nRemove,:);        % Trim points to make an even multiple of nbin
Avg = mean(reshape(a,nbin,[],ncol));
AvgF = squeeze(Avg);
